package com.ruoyi.web.controller.examine.goods;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.NormalExamine;
import com.ruoyi.common.core.domain.entity.NormalExamineDetail;
import com.ruoyi.common.core.domain.entity.RestaurantOrder;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.NormalExamineDetailService;
import com.ruoyi.system.service.NormalExamineService;
import com.ruoyi.system.service.RestaurantOrderService;
import com.ruoyi.system.service.resp.NormalExamineDTO;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/examine/goods/normalExamine")
public class NormalExamineController extends BaseController {
    @Resource
    private NormalExamineService normalExamineService;

    @Resource
    private NormalExamineDetailService normalExamineDetailService;

    @Resource
    private RestaurantOrderService restaurantOrderService;

    @Log(title = "查询点餐质检", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(NormalExamine normalExamine)
    {
        startPage();
        List<NormalExamine> list = normalExamineService.selectList(normalExamine);
        return getDataTable(fillToDTO(list));
    }

    @Log(title = "查询点餐质检", businessType = BusinessType.OTHER)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(normalExamineService.selectById(id));
    }

    @Log(title = "点餐质检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody NormalExamine examinePlan) {
        examinePlan.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = normalExamineService.update(examinePlan);
        if (result) {
            return success();
        }
        return error();
    }

    private List<NormalExamineDTO> fillToDTO(List<NormalExamine> normalExamines) {
        if (CollectionUtils.isEmpty(normalExamines)) {
            return new ArrayList<>();
        }
        List<NormalExamineDTO> normalExamineDTOS = normalExamines.stream().map(n -> {
            RestaurantOrder restaurantOrder = restaurantOrderService.selectNormalOrderById(n.getOrderId());
            NormalExamineDTO normalExamineDTO = new NormalExamineDTO();
            normalExamineDTO.setId(n.getId());
            normalExamineDTO.setOrderId(n.getOrderId());
            normalExamineDTO.setStatus(n.getStatus());
            normalExamineDTO.setCreateBy(n.getCreateBy());
            normalExamineDTO.setCreateTime(n.getCreateTime());
            normalExamineDTO.setUpdateBy(n.getUpdateBy());
            normalExamineDTO.setUpdateTime(n.getUpdateTime());
            normalExamineDTO.setRemark(n.getRemark());
            normalExamineDTO.setInspector(n.getInspector());
            normalExamineDTO.setCheckTime(n.getCheckTime());
            normalExamineDTO.setOrderNum(Objects.nonNull(restaurantOrder) ? restaurantOrder.getOrderNum() : "");
            PageUtils.clearPage();
            List<NormalExamineDetail> normalExamineDetails = normalExamineDetailService.selectList(NormalExamineDetail.builder().examineId(n.getId()).build());
            if (CollectionUtils.isEmpty(normalExamineDetails)) {
                normalExamineDTO.setStatus("无质检内容");
            } else {
                Integer okCnt = 0;
                Integer failCnt = 0;
                Integer waitCnt = 0;
                for (NormalExamineDetail detail : normalExamineDetails) {
                    if (detail == null) {
                        continue;
                    }
                    if (StringUtils.isBlank(detail.getPlateStatus())
                        && StringUtils.isBlank(detail.getResourceStatus())) {
                        waitCnt = waitCnt + 1;
                    } else if ("否".equals(detail.getPlateStatus())
                        || "否".equals(detail.getResourceStatus())) {
                        failCnt = failCnt + 1;
                    } else if ("是".equals(detail.getPlateStatus())
                        && "是".equals(detail.getResourceStatus())) {
                        okCnt = okCnt + 1;
                    }
                }
                if (failCnt > 0) {
                    normalExamineDTO.setStatus("质检不合格");
                } else if (okCnt.equals(normalExamineDetails.size())) {
                    normalExamineDTO.setStatus("质检合格");
                } else {
                    normalExamineDTO.setStatus("质检待完成");
                }
                if (StringUtils.isNotBlank(n.getStatus()) && n.getStatus().equals("取消")) {
                    normalExamineDTO.setStatus("取消");
                }
            }

            return normalExamineDTO;
        }).collect(Collectors.toList());
        PageInfo<NormalExamine> oldPage = new PageInfo<>(normalExamines);
        Page<NormalExamineDTO> newPage = new Page<>();
        newPage.addAll(normalExamineDTOS);
        newPage.setTotal(oldPage.getTotal());
        return newPage;
    }
}
